// 日期格式化
const formatTime = date => {
	if (date == undefined ) date = new Date()
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

// 格式化两位数字，一位数时前面添0，主要供日期格式化函数使用
const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : '0' + n
}

const request = (url, type, param, callback) => {
	
	wx.request({
		url: url,
		method: type,
		data: param,
		dataType: 'json',
		success: res => {
			console.log("post sucessed")
			callback(res)
		},
		fail: res => {
			console.log(res)
		},
		complete: res => {
			// console.log("posted")
		}
	})
}

const get = (url, param, callback) => {
	console.log('try to getting ' + url)
	console.log(new Date().getTime())
	request(url, 'GET', param, callback)
}

const post = (url, param, callback) => {
	console.log('try to post ' + url)
	request(url, 'POST', param, callback)
}

module.exports = {
  formatTime, request, post, get
}
